﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using NTQOnlineExam.Services.Infrastructure;
using NTQOnlineExam.Services.Models;
using NTQOnlineExam.Services.ReportService.Account;
using NTQOnlineExam.Services.ReportService.Section;
using NTQOnlineExam.Services.ReportService.Term;
using NTQOnlineExam.Services.ReportService.Year;
using NTQOnlineExamFrontend.Models;

namespace NTQOnlineExamFrontend.Controllers
{
  //[Authorize]
  public class TermController : Controller
  {
    private readonly ITermReportService _termReportService;
    private readonly ISectionReportService _sectionReportService;
    private readonly IAccountReportService _accountReportService;
    private readonly IYearReportService _yearReportService;
    public TermController(ITermReportService termReportService, ISectionReportService sectionReportService, IAccountReportService accountReportService, IYearReportService yearReportService)
    {
      _termReportService = termReportService;
      _sectionReportService = sectionReportService;
      _accountReportService = accountReportService;
      _yearReportService = yearReportService;
    }

    [ChildActionOnly]
    public ActionResult TermYearMenu()
    {
      TermYearViewModel model = new TermYearViewModel { Years = _yearReportService.GetAll() };
      return PartialView("_TermYearList", model);
    }

    [HttpGet]
    [AllowAnonymous]
    public ActionResult GetTerms(int? year, int page = 0)
    {
      var currentUser = _accountReportService.GetCurrentUser();
      var termByYear = _termReportService.GetTermsByYear(year, page, new TermInfoViewModel().PageSize, currentUser == null ? 0 : (int)currentUser.ProviderUserKey);
      var terms = termByYear.Items;
      var termIds = terms.Select(x => x.TermId).ToList();
      var sections = _sectionReportService.GetSectionByTermList(termIds);

      var yearName = _yearReportService.GetById(year ?? 0);
      var className = yearName != null ? yearName.Name : string.Empty;

      IList<BreadcrumItem> breadcrumItems = new List<BreadcrumItem>();
      breadcrumItems.Add(new BreadcrumItem("Shop", "/Term/GetTerms", false));

      if (!string.IsNullOrEmpty(className))
      {
        breadcrumItems.Add(new BreadcrumItem("Examination Type", "/Term/GetTerms", string.IsNullOrEmpty(className)));
        breadcrumItems.Add(new BreadcrumItem(className, string.Empty, true));
      }
      else
        breadcrumItems.Add(new BreadcrumItem("Available Examinations", "/Term/GetTerms", string.IsNullOrEmpty(className)));
      ViewData["Breadcrumb"] = breadcrumItems;

      return View("TermIndex", new TermInfoViewModel { TermByYear = terms, Count = termByYear.Count, Sections = sections, SelectedPage = page });
    }

    public ActionResult GetTermDetail(int termId, string year)
    {
      var currentUser = _accountReportService.GetCurrentUser();
      var termDto = _termReportService.GetById(termId, currentUser == null ? 0 : (int)currentUser.ProviderUserKey);

      IList<BreadcrumItem> breadcrumItems = new List<BreadcrumItem>();
      breadcrumItems.Add(new BreadcrumItem("Shop", "/Term/GetTerms", false));
      breadcrumItems.Add(new BreadcrumItem("Available Examinations", "/Term/GetTerms", false));
      breadcrumItems.Add(new BreadcrumItem(termId.ToString(), string.Empty, true));
      ViewData["Breadcrumb"] = breadcrumItems;

      var sections = _sectionReportService.GetSections(termId);

      return View("TermDetail", new TermDetailViewModel() { Term = termDto, Sections = sections, ClassYear = year });
    }

  }
}
